package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.SQLOutput;
import java.util.List;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    public SetmealDishService setmealDishService;
    @Autowired
    public SetmealService setmealService;
    @Autowired
    public CategoryService categoryService;
    @Autowired
    public ShoppingCartService shoppingCartService;
    @Resource
    private EmployeeService employeeService;

    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info("套餐信息：{}",setmealDto);
        setmealService.saveWithDish(setmealDto);
        return R.success("新增套餐成功");
    }

    @GetMapping("/page")
    public R<Page> get(int page,int pageSize,String name){
        Page<Setmeal> setmealPage=new Page<>(page,pageSize);
        Page<SetmealDto> setmealDtoPage=new Page<>(page,pageSize);
        LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(name!=null,Setmeal::getName,name);
        queryWrapper.orderByAsc(Setmeal::getUpdateTime);
        setmealService.page(setmealPage,queryWrapper);
        BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");
        List<Setmeal> records = setmealPage.getRecords();
        List<SetmealDto> d= records.stream().map((tmp)->{
            SetmealDto setmealDto=new SetmealDto();
            BeanUtils.copyProperties(tmp,setmealDto);
            Long categoryId = tmp.getCategoryId();
            Category category = categoryService.getById(categoryId);
            setmealDto.setCategoryName(category.getName());
            return setmealDto;
        }).collect(Collectors.toList());
        setmealDtoPage.setRecords(d);
        return R.success(setmealDtoPage);
    }


    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getCreateTime);
        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }

    /**
     * 停售与起售
     * @param s
     * @param ids
     * @return
     */
    @PostMapping("/status/{s}")
    public R<String> post(@PathVariable int s,@RequestParam Long[] ids){
        log.info(ids.toString());
        for (Long id : ids) {
            LambdaQueryWrapper<Setmeal> wrapper=new LambdaQueryWrapper<>();
            wrapper.eq(Setmeal::getId,id);
            Setmeal setmeal = setmealService.getOne(wrapper);
            Integer status = setmeal.getStatus();
            status=s;
            setmeal.setStatus(status);
            setmealService.updateById(setmeal);
        }
        return R.success("修改成功");
    }

    /**
     * 删除与批量删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam Long[] ids){
        log.info(ids.toString());
        LambdaQueryWrapper<SetmealDish> wrapper=new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>();
        for (Long id : ids) {
            wrapper.eq(SetmealDish::getSetmealId,id);
            setmealDishService.remove(wrapper);

            queryWrapper.eq(Setmeal::getId,id);
            setmealService.remove(queryWrapper);
        }
        return R.success("删除成功");
    }

    /**
     * 修改查看套餐
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R seTmEal(@PathVariable Long id){
        log.info("{}:",id);
        LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal::getId,id);
        Setmeal setmeal = setmealService.getOne(queryWrapper);
        LambdaQueryWrapper<SetmealDish> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        List<SetmealDish> list = setmealDishService.list(wrapper);
        SetmealDto setmealDto=new SetmealDto();

        setmealDto.setSetmealDishes(list);
        setmealDto.setId(setmeal.getId());
        setmealDto.setCategoryId(setmeal.getCategoryId());
        setmealDto.setName(setmeal.getName());
        setmealDto.setPrice(setmeal.getPrice());
        setmealDto.setStatus(setmeal.getStatus());
        setmealDto.setCode(setmeal.getCode());
        setmealDto.setDescription(setmeal.getDescription());
        setmealDto.setImage(setmeal.getImage());

        LambdaQueryWrapper<Employee> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Employee::getId,BaseContext.getCurrentId());
        Employee employee = employeeService.getOne(lambdaQueryWrapper);
        setmealDto.setCategoryName(employee.getName());
        return R.success(setmealDto);
    }

    @Transactional
    @PutMapping()
    public R baoCun(@RequestBody SetmealDto setmealDto){
        log.info("{}:",setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        for (SetmealDish setmealDish : setmealDishes) {
            setmealDishService.updateById(setmealDish);
        }
        setmealService.updateById(setmealDto);
        return R.success("ok");
    }

}
